package com.ddd.wandemo;

import android.graphics.Color;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.base.NormalBaseActivity;
import com.r0adkll.slidr.Slidr;
import com.r0adkll.slidr.model.SlidrConfig;
import com.r0adkll.slidr.model.SlidrPosition;
import com.util.ToastUtil;



public class ArticleDetailWebView extends NormalBaseActivity {


    public static final int TYPE_ARTICLE = 10000;
    public static final int TYPE_HOME = 10001;
    private WebView mWebView;
    private ProgressBar mProgressBar;
    private String mTitle;
    private String mLink;
    private Toolbar mToolbar;
    private int mType;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_article_detail;
    }


    @Override
    protected void initView() {
        mWebView = findViewById(R.id.wv_article_detail);

        mProgressBar = findViewById(R.id.loadProgressBar);
        mToolbar = findViewById(R.id.toolbar);

        SlidrConfig config = new SlidrConfig.Builder()
                .primaryColor(getResources().getColor(R.color.app_background))
//                .primaryColor(Color.TRANSPARENT)
                .secondaryColor(getResources().getColor(R.color.app_background))
                .position(SlidrPosition.LEFT)
                .sensitivity(1f)
                .scrimColor(Color.BLACK)
                .scrimStartAlpha(0.8f)
                .scrimEndAlpha(0f)
                .velocityThreshold(5f)
                .distanceThreshold(0.1f)
                .edge(false)
                .edgeSize(0.18f) // The % of the screen that counts as the edge, default 18%
                .build();

        Slidr.attach(this, config);
    }


    @Override
    protected void initEvent() {
        mLink = getIntent().getStringExtra("link");
        mTitle = getIntent().getStringExtra("title");
        mType = getIntent().getIntExtra("type", 0);

        initToolBar();
        initWebView(mLink);

    }

    private void initToolBar() {
        mToolbar.setTitle(R.string.loading);
        //设置返回键
        mToolbar.setNavigationIcon(R.drawable.ic_chevron_up_grey600_24dp);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //设置右上角menu
        if (mType == TYPE_ARTICLE) {
            getMenuInflater().inflate(R.menu.normal_toolbar_menu, menu);
        }
        return super.onCreateOptionsMenu(menu);
    }


    private void initWebView(String link) {
        mWebView.getSettings().setJavaScriptEnabled(true); //js交互
        mWebView.getSettings().setSupportZoom(true); //是否支持缩放
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.setWebChromeClient(new MyWebChromeClient());

        //        HashMap localHashMap = new HashMap();
        //        localHashMap.put("Referer", "http://lottery88.cn");

        //        mWebView.addJavascriptInterface(this, "AndroidWebView");
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        if (!TextUtils.isEmpty(link)) {
            mWebView.loadUrl(link);
        }
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
    }


    private class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            if (progress == 100) {
                mProgressBar.setVisibility(View.GONE);
            } else {
                if (View.INVISIBLE == mProgressBar.getVisibility()) {
                    mProgressBar.setVisibility(View.VISIBLE);
                }
                mProgressBar.setProgress(progress);
            }
            super.onProgressChanged(view, progress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            mToolbar.setTitle(title);
            super.onReceivedTitle(view, title);
        }
    }

    //toolbar按钮点击时间
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_collect:
                ToastUtil.showShort(ArticleDetailWebView.this, item.getTitle());
                break;
            case R.id.action_share:
                ToastUtil.showShort(ArticleDetailWebView.this, item.getTitle());
                break;
            case R.id.action_copy_link:
                ToastUtil.showShort(ArticleDetailWebView.this, item.getTitle());
                break;
            case R.id.action_open_link:
                ToastUtil.showShort(ArticleDetailWebView.this, item.getTitle());
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            ViewParent parent = mWebView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(mWebView);
            }
            // 退出时调用此方法，移除绑定的服务，否则某些特定系统会报错
            mWebView.getSettings().setJavaScriptEnabled(false);

            mWebView.stopLoading();
            mWebView.clearHistory();
            mWebView.removeAllViews();
            mWebView.destroy();
        }
        super.onDestroy();
    }
}
